package tools;

import java.sql.*;
import java.util.ArrayList;

public class Catalog {

    private Statement stmt = null;
    private Connection conn = null;
    private ResultSet rs = null;

    public void connect() {
        conn = KoneksiDataBase.getConnection();
    }

    /*
     * method dibawah digunakan untuk mencari buku yang ada di data base dengan memasukkan keyword.
     */
    public ArrayList<Buku> getBooksCatalogSearch(String title) {
        String sql;
        ArrayList vCatalog = new ArrayList<Buku>();
        try {
            sql = "SELECT * from buku WHERE judul LIKE '%" + title + "%' OR pengarang LIKE '%"
                    + title + "%' OR penerbit LIKE '%" + title + "%' ORDER BY KODE_BUKU DESC";
            connect();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                Buku book = new Buku();
                book.setJudul(rs.getString("judul"));
                book.setPengarang(rs.getString("pengarang"));
                book.setPenerbit(rs.getString("penerbit"));
                book.setJumlah_baca(rs.getInt("jumlah_baca"));
                book.setCover(rs.getString("cover"));
                book.setPdf(rs.getString("pdf"));
                vCatalog.add(book);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return vCatalog;
    }

    /*
     * method dibawah digunakan untuk menampilkan daftar buku yang ada pada database.
     */
    public ArrayList<Buku> getDisplay() {
        String sql;
        ArrayList vCatalog = new ArrayList<Buku>();
        try {
            sql = "SELECT * from Buku ORDER BY KODE_BUKU DESC";
            connect();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                Buku book = new Buku();
                book.setJudul(rs.getString("judul"));
                book.setPengarang(rs.getString("pengarang"));
                book.setPenerbit(rs.getString("penerbit"));
                book.setJumlah_baca(rs.getInt("jumlah_baca"));
                book.setCover(rs.getString("cover"));
                book.setPdf(rs.getString("pdf"));
                vCatalog.add(book);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return vCatalog;
    }
}

